package com.khoinguyen.feedtweet;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;

import com.khoinguyen.feedtweet.model.VOUser;

public class FeedTweetMain {
	public static void main(String[] args) throws IOException, TwitterException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = factory.openSession();
		Twitter twitter = TwitterFactory.getSingleton();
		long cursor = -1;
		IDs ids;
		do {
			ids = twitter.getFollowersIDs("ndkhoiits", cursor);
			long t = ids.getIDs().length;
			System.out.println(t);
			for (long id : ids.getIDs()) {
				User user = twitter.showUser(id);
				System.out.println(user.getScreenName());
				VOUser voUser = new VOUser(user.getId(), user.getScreenName(), user.getName());
				session.insert("insertTwitterUser", voUser);
			}
		} while ((cursor = ids.getNextCursor()) != 0);
		session.commit();
		session.close();
	}
}
